pipeline {
  agent {
    kubernetes {
      label 'var-web-image-build'
      defaultContainer 'ruby-node-grunt'
      yaml """
apiVersion: v1
kind: Pod
metadata:
  labels:
    image-build: var-web-v4
spec:
  containers:
  - name: ruby-node-grunt
    image: utility.apps.DOMAIN.EX/base-images/ruby-node-grunt:1.0.1
    env:
    - name: DOCKER_HOST
      value: tcp://localhost:2375
    command:
    - cat
    tty: true
  - name: docker
    image: docker:1.12.6
    env:
    - name: DOCKER_HOST
      value: tcp://localhost:2375
    command:
    - cat
    tty: true
  - name: dind-daemon
    image: utility.apps.DOMAIN.EX/base-images/map-dind-image:1.0.1
    securityContext:
      privileged: true
    ports:
    - containerPort: 2375
    volumeMounts:
    - name: docker-graph-storage
      mountPath: /var/lib/docker
    tty: true
  - name: k8-deploy
    image: utility.apps.DOMAIN.EX/base-images/k8-deploy:1.0.4
    command:
    - cat
    tty: true
  imagePullSecrets:
  - name: map-dtr
  volumes:
  - name: docker-graph-storage
    emptyDir: {}
"""
    }
  }

  environment { 
      SERVICE_VERSION = '4.6.0'
      IMAGE = '/var/var-web'
      DTR_URL = 'utility.apps.DOMAIN.EX'
      DTR_PREFIX = 'utility.apps.DOMAIN.EX'
      DTR_ORG = '/base-images'
  }


  stages {

    stage('App Build') {
      steps {
        withCredentials([string(credentialsId: 'VA_NEXUS_PWD', variable: 'VA_NEXUS_PWD'), string(credentialsId: 'VA_NEXUS_USER', variable: 'VA_NEXUS_USER'), string(credentialsId: 'MAP_DTR_PWD', variable: 'MAP_DTR_PWD'), string(credentialsId: 'MAP_DTR_USER', variable: 'MAP_DTR_USER')]) {
            container('ruby-node-grunt') {
              sh './build.sh'
            }
        }
      }
    }

    stage('Image Build and Push') {
      steps {
        withCredentials([string(credentialsId: 'MAP_DTR_PWD', variable: 'MAP_DTR_PWD'), string(credentialsId: 'MAP_DTR_USER', variable: 'MAP_DTR_USER')]) {
            container('docker') {
              sh 'docker login -u $MAP_DTR_USER -p $MAP_DTR_PWD utility.apps.DOMAIN.EX'
              sh 'cd dist/ && docker build -t $DTR_URL$IMAGE:$SERVICE_VERSION .'
              sh 'docker push $DTR_URL$IMAGE:$SERVICE_VERSION'
            }
        }
      }
    }

    stage('Deploy to Staging'){

        environment {
            K8_API_ADDR = 'https://internal-staging-kube-api-336601082.us-gov-west-1.elb.amazonaws.com:6443'
            VAMF_ENVIRONMENT = 'map-staging'
            VAULT_ADDR = 'http://ip-172-16-2-10.us-gov-west-1.compute.internal:8200'
            CONSUL_ADDR = 'ip-172-16-2-11.us-gov-west-1.compute.internal:8500'
            SECRET_PATH = 'secret/map-staging/deploy'
            CONSUL_PATH = 'appconfig/map-staging/deploy'
            NAMESPACE = 'default'
        }

        steps {
            withCredentials([string(credentialsId: 'CONSUL_MASTER_TOKEN', variable: 'CONSUL_TOKEN')]) {
                container('k8-deploy') {
                    sh 'echo "Deploying $DTR_URL$IMAGE:$VERSION"'
                    sh '/entrypoint.sh'
                }
            }
        }
    }

  }

  post {
    always {
      cleanWs() /* clean up our workspace */
    }
  }
}TTTTTT